package com.xyy.ec.pop.api.kingdinggmp.utils;


import com.xyy.ec.pop.api.kingdinggmp.config.ComentCfg;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;

public class CfgUtils {
    static String cfgFilePath = null;
    static ComentCfg instance;

    public CfgUtils() {
    }

    static void setCfgFilePath(String cfgPath) {
        cfgFilePath = cfgPath;
    }

    public static ComentCfg getAppDefaultCfg() {
        if (instance != null) {
            return instance;
        } else {
            ComentCfg cfg = null;
            Properties properties = new Properties();
            File file = null;
            boolean isLoad = false;

            try {
                if (cfgFilePath != null) {
                    file = new File(cfgFilePath);
                    PrintUtils.print("优先使用用户自定义的配置文件路径: " + file.getPath());
                } else {
                    file = new File((new File(".")).getCanonicalPath() + "/kdwebapi.properties");
                    PrintUtils.print("默认优先读取根目录下的配置文件->" + file.getPath());
                }

                if (file != null && file.exists()) {
                    InputStream inputStream = new FileInputStream(file.getPath());
                    properties.load(new InputStreamReader(inputStream, "utf-8"));
                    inputStream.close();
                    isLoad = true;
                } else {
                    InputStream inputStream = CfgUtils.class.getResourceAsStream("/kdwebapi.properties");
                    URL configUrl = CfgUtils.class.getClassLoader().getResource("kdwebapi.properties");
                    PrintUtils.print("继续在资源中寻找配置文件->" + (configUrl != null ? URLDecoder.decode(configUrl.getPath(), "utf-8") : "在bin目录下找不到配置文件"));
                    if (inputStream != null) {
                        properties.load(new InputStreamReader(inputStream, "utf-8"));
                        inputStream.close();
                        isLoad = true;
                    }
                }
            } catch (IOException var9) {
                var9.printStackTrace();
            }

            if (isLoad) {
                ComentCfg scfg = new ComentCfg();
                String svr = properties.getProperty("X-pop-orgId");
                if (svr != null) {
                    scfg.setPopOrgId(svr);
                }
                scfg.setPopAppId(properties.getProperty("X-pop-appId"));
                scfg.setPopAppSecret(properties.getProperty("X-pop.appSecret"));
                String isFullSync = properties.getProperty("isFullSync");
                String isDone = properties.getProperty("isDone");
                if (isFullSync != null) {
                    scfg.setIsFullSync(Boolean.valueOf(isFullSync));
                }
                if (isDone != null) {
                    scfg.setIsDone(Boolean.valueOf(isDone));
                }
                scfg.setCustomerSale(properties.getProperty("CustomerSale"));
                scfg.setSyncWarehouse(properties.getProperty("SyncWarehouse"));
                scfg.setOrderBus(properties.getProperty("OrderBus"));
                scfg.setOrderSales(properties.getProperty("OrderSales"));
                scfg.setCustomerGroup(properties.getProperty("CustomerGroup"));
                cfg = scfg;
            }
            instance = cfg;
            return instance;
        }
    }
}
